package org.csu.pet_store_back.service.Impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.csu.pet_store_back.config.AlipayConfig;
import org.csu.pet_store_back.domain.AlipayVo;
import org.csu.pet_store_back.service.PayService;
import org.springframework.stereotype.Service;

@Service("PayService")
public class PayServiceImpl implements PayService {
    @Override
    public Object Alipay(AlipayVo alipayVo) throws Exception {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayConfig.gateway,
                AlipayConfig.app_id,
                AlipayConfig.merchant_private_key,
                "json",
                AlipayConfig.charset,
                AlipayConfig.alipay_public_key,
                AlipayConfig.sign_type
        );
        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        alipayRequest.setBizContent("{\"out_trade_no\":\""+alipayVo.getOrderNo() +"\","
                + "\"total_amount\":\""+ alipayVo.getTotalCost() +"\","
                + "\"subject\":\""+ alipayVo.getOrderTitle() +"\","
                + "\"body\":\""+ alipayVo.getDescription() +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        return alipayClient.pageExecute(alipayRequest).getBody();
    }
}
